package com.aios.action.settings.switchPanel;

import org.apache.commons.validator.routines.UrlValidator;
import org.apache.log4j.Logger;

public class SwitchPanelSaveAction extends AbstractSwitchPanelAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger
			.getLogger(SwitchPanelSaveAction.class);

	public String execute() {

		validateSwitchPanel();

		if (actionError != null && !actionError.equals("")) {
			return INPUT;
		}

		if (getMode() == Mode.ADD) {
			// TODO, change the logic
		} else if (getMode() == Mode.EDIT) {

			try {
				switchPanelService.update(switchPanel);
				actionMessage = getText("success.update");
			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.update");
				return INPUT;
			}
		}

		return SUCCESS;
	}

	private void validateSwitchPanel() {
		/*if (!switchPanel.getUrl().equals("")) {

			try {
				UrlValidator urlValidator = new UrlValidator();
				if (!urlValidator.isValid(switchPanel.getUrl())) {
					actionError = getText("error.switch.panel.url.invalid");
				}
			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.switch.panel.url.invalid");
			}
		}*/

	}

}
